﻿//配置插件目录
layui.config({
    base: '/mods/'
});
layui.use(['table', 'laypage', 'layer', 'form', 'util'], function () {
    var table = layui.table;
    var $ = layui.jquery;
    var util = layui.util;
    var layer = layui.layer;
    var form = layui.form;
    // 获取所有的 li 元素
    var liElements = document.getElementsByClassName('layui-tab-title')[0].getElementsByTagName('li');
    // 为每个 li 元素添加点击事件监听器
    for (var i = 0; i < liElements.length; i++) {
        liElements[i].addEventListener('click', function () {
            // 获取当前点击的 li 元素的 lay-id 属性值
            var layId = this.getAttribute('lay-id');

            // 将 lay-id 值作为参数传递给下层方法
            GetMyEventData(layId);
        });
    }

    function GetMyEventData(layId) {

        //获取查询条件
        var EventType = $('#EventTypeID').val();
        var EventLevel = $('#EventLevelID').val();
        var EventSource = $('#EventSourceID').val();
        var Flog = layId;
        //ajax后台交互
        $.ajax({
            url: 'http://8.140.245.229:8000/SingleRead/api/EventY/GetEmergency',
            type: 'get',
            dataType: 'json',
            //参数
            data: {
                EventType: EventType,
                EventSource: EventSource,
                EventLevel: EventLevel,
                Flog: Flog
            },
            success: function (res) {
                //渲染表格数据
                layui.use(['table'], function () {
                    var table = layui.table;
                    table.render({
                        //挂载的表格id
                        elem: '#ID-table-demo-setRowChecked',
                        cols: [[
                            { type: 'numbers', title: '序号', width: 120, align: 'center' },
                            { field: 'EventDesc', title: '事件描述', width: 120, align: 'center' },
                            {
                                field: 'EventLevel', title: '事件级别', width: 120, align: 'center',
                                templet: function (d) {
                                    if (d.EventLevel == 1) {
                                        return '<span>一般</span>'
                                    } else if (d.EventLevel == 2) {
                                        return '<span>紧急</span>'
                                    }
                                }
                            },
                            { field: 'Reporter', title: '上报人', width: 120, align: 'center' },
                            {
                                field: 'ReportDate', title: '上报时间', width: 120, align: 'center',
                                templet: function (data) { return data.ReportDate.substring(0, 16).replace("T", " ") }
                            },
                            {
                                field: 'ArriveDate', title: '到达时间', width: 120, align: 'center',
                                templet: function (data) { return data.ArriveDate.substring(0, 16).replace("T", " ") }
                            },
                            {
                                field: 'EndTime', title: '处理截止时间', width: 120, align: 'center',
                                templet: function (data) { return data.EndTime.substring(0, 16).replace("T", " ") }
                            },
                            {
                                field: 'WarningState', title: '预警状态', width: 120, align: 'center',
                                templet: function (d) {
                                    if (d.WarningState == 1) {
                                        return '<i class="layui-icon layui-icon-circle-dot" style="font-size: 20px; color: #16b777;"></i>'
                                    } else if (d.WarningState == 2) {
                                        return '<i class="layui-icon layui-icon-circle-dot" style="font-size: 20px; color: #ff5722"></i>'
                                    }
                                }
                            },
                            {
                                field: 'NodeTypeEnum', title: '当前节点', width: 120, align: 'center', templet: function (d) {
                                    if (d.NodeTypeEnum == 1) {
                                        return '<span>立案审核</span>'
                                    } else if (d.NodeTypeEnum == 2) {
                                        return '<span>现场核实</span>'
                                    } else if (d.NodeTypeEnum == 3) {
                                        return '<span>案件核查</span>'
                                    } else if (d.NodeTypeEnum == 4) {
                                        return '<span>案件处置</span>'
                                    }
                                }
                            },
                            { title: '操作', width: 120, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true },
                        ]],
                        data: res,

                    })
                })
                AddLogInfo("查询待办事件", "", "");

            }
        })
    }



    // 监听工具条，实现删除功能
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event == 'edit') {
            location.href = `/EvenHand/UpdEventView?id=${data.EventId}`;
        }
    });


    //事件类型
    function GetEventType() {
        $.ajax({
            url: "http://8.140.245.229:8000/SingleRead/api/EventY/GetEventType",
            async: false,
            type: "get",
            success: function (res) {
                var select = $("#EventTypeID");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.name).text(item.name))
                })
                layui.form.render('select');
            }
        })
    }

    //事件级别
    function GetEventLevel() {
        $.ajax({
            url: "http://8.140.245.229:8000/SingleRead/api/EventY/GetEventLevel",
            async: false,
            type: "get",
            success: function (res) {
                var select = $("#EventLevelID");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.name).text(item.name))
                })
                layui.form.render('select');
            }
        })
    }

    //事件来源
    function GetEventSource() {

        $.ajax({
            url: "http://8.140.245.229:8000/SingleRead/api/EventY/GetEventSource",
            async: false,
            type: "get",
            success: function (res) {
                var select = $("#EventSourceID");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.name).text(item.name))
                })
                layui.form.render('select');
            }
        })
        //$.ajax({
        //    url: "http://8.140.245.229:8000/SingleRead/api/EventY/GetEventSource",
        //    async: false,
        //    type: "get",
        //    success: function (res) {
        //        var select = $("#EventSource");
        //        $.each(res, function (index, item) {
        //            select.append($('<option>').val(item.name).text(item.name))
        //        })
        //        layui.form.render('select');
        //    }
        //})
    }

    //第一个参数为操作内容，例如：登录了系统，修改/删除/添加/查询了日志表（或其他表，每次调用根据进行的操作写）
    //第二个参数为修改专用，将修改前的参数转为json字符串格式传入
    //第三个参数是将添加或删除的数据转为json传入，若是修改方法则把修改后的数据转为json字符串传入
    function AddLogInfo(OperationalContext, PreOperation, AfterOperation) {
        const data = {
            olContext: OperationalContext,
            PreOperation: PreOperation,
            AfterOperation: AfterOperation
        };
        // alert(JSON.stringify(data));
        $.ajax({
            url: "http://8.140.245.229:8000/WorkBench/api/Log/AddLog",
            type: 'post',
            dataType: 'json',
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            contentType: 'application/json', // 添加正确的 Content-Type 头部
            data: JSON.stringify(data),
            success: function (res) {
                // alert(res)
               //getListData(1);
                console.log(res)
            }
        })
    }

    //钩子函数
    $(function () {
        // 初始化加载数据
        GetMyEventData();
        GetEventType();
        GetEventLevel();
        GetEventSource();
    })

    $('#searchbtn').on('click', function () {
        GetMyEventData();
    });

});
